/*
 * @Author: zxh
 * @Date: 2024-08-14 11:06:00
 * @LastEditors: zxh
 * @LastEditTime: 2024-08-14 11:46:01
 * @Description: 请填写简介
 */
import { computed, onMounted, ref } from 'vue'
import dayjs from 'dayjs'
export const useCountDown = () => {
  // 1.响应式数据 2.倒计时函数
  const time = ref(0)
  let timer: any = null
  // 格式化时间为 **分 **秒
  const formatTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))
  // 开启倒计时函数
  const start = (currentTime: number) => {
    time.value = currentTime
    timer = setInterval(() => {
      time.value--
    }, 1000)
  }
  //组件销毁时清楚定时器
  onMounted(() => {
    timer && clearInterval(timer)
  })
  return {
    formatTime,
    start
  }
}
